<template>
  <div class="follow-page">
    <!-- 顶部导航栏 -->
    <div class="header">
      <button class="back-btn" @click="goBack">
        <svg
            xmlns="http://www.w3.org/2000/svg"
            width="24"
            height="24"
            viewBox="0 0 24 24"
            fill="none"
            stroke="currentColor"
            stroke-width="2"
            stroke-linecap="round"
            stroke-linejoin="round"
        >
          <polyline points="15 18 9 12 15 6"></polyline>
        </svg>
      </button>
      <div class="tab-buttons">
        <button
            :class="['tab-btn', { active: activeTab === 'follow' }]"
            @click="activeTab = 'follow'"
        >
          关注
        </button>
        <button
            :class="['tab-btn', { active: activeTab === 'fans' }]"
            @click="activeTab = 'fans'"
        >
          粉丝
        </button>
      </div>
    </div>

    <!-- 搜索栏 -->
    <div class="search-bar">
      <input
          type="text"
          placeholder="输入小黑盒昵称搜索"
          v-model="searchKeyword"
          class="search-input"
      />
      <button class="search-clear-btn" @click="searchKeyword = ''" v-if="searchKeyword">
        <svg
            xmlns="http://www.w3.org/2000/svg"
            width="20"
            height="20"
            viewBox="0 0 24 24"
            fill="none"
            stroke="currentColor"
            stroke-width="2"
            stroke-linecap="round"
            stroke-linejoin="round"
        >
          <line x1="18" y1="6" x2="6" y2="18"></line>
          <line x1="6" y1="6" x2="18" y2="18"></line>
        </svg>
      </button>
    </div>

    <!--    &lt;!&ndash; 在线状态筛选 &ndash;&gt;-->
    <!--    <div class="online-filter">-->
    <!--      <button class="filter-btn">在线状态</button>-->
    <!--    </div>-->

    <!-- 列表内容区域 -->
    <div class="list-content">
      <div v-if="activeTab === 'follow'" class="follow-list">
        <div v-if="filteredFollowList.length === 0 && !searchKeyword">
          暂无关注用户，快去关注感兴趣的人吧～
        </div>
        <div v-else-if="filteredFollowList.length === 0 && searchKeyword">
          没有找到与"{{ searchKeyword }}"相关的关注用户
        </div>
        <ul class="user-list">
          <li
              v-for="(user, index) in filteredFollowList"
              :key="index"
              class="user-item"
          >
            <img :src="user.user.avatar" alt="用户头像" class="user-avatar" />
            <div class="user-info">
              <div class="user-name-level">
                <span class="name">{{ user.user.nickname }}</span>
                <span class="level-tag" :style="{ backgroundColor: user.levelTagBg }">{{ user.levelTag }}</span>
              </div>
              <span class="status" v-if="user.user.status==1">正常</span>
              <span class="status" v-else-if="user.user.status==0">禁言</span>
              <span class="status" v-else-if="user.user.status==-1">封禁</span>
            </div>
            <button class="follow-btn" :class="{ followed: user.isFollowed }" @click="toggleFollow(user, index)">
              {{ user.isFollowed ? '已关注' : '关注' }}
            </button>
          </li>
        </ul>
      </div>
      <div v-else class="fans-list">
        <div v-if="filteredFansList.length === 0 && !searchKeyword">
          暂无粉丝，继续加油获得更多粉丝吧～
        </div>
        <div v-else-if="filteredFansList.length === 0 && searchKeyword">
          没有找到与"{{ searchKeyword }}"相关的粉丝
        </div>
        <ul class="user-list">
          <li
              v-for="(user, index) in filteredFansList"
              :key="index"
              class="user-item"
          >
            <img :src="user.user.avatar" alt="用户头像" class="user-avatar" />
            <div class="user-info">
              <div class="user-name-level">
                <span class="name">{{ user.user.nickname }}</span>
                <span class="level-tag" :style="{ backgroundColor: user.levelTagBg }">{{ user.levelTag }}</span>
              </div>
              <span class="status" v-if="user.user.status==1">正常</span>
              <span class="status" v-else-if="user.user.status==0">禁言</span>
              <span class="status" v-else-if="user.user.status==-1">封禁</span>
            </div>
            <button class="follow-btn" :class="{ followed: user.isFollowed }" @click="toggleFollow(user, index)">
              {{ user.isFollowed ? '已关注' : '关注' }}
            </button>
          </li>
        </ul>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, watch, onMounted } from 'vue';
import { useRoute, useRouter } from "vue-router";
import axios from "@/utils/axios.ts";

const route = useRoute();
const router = useRouter();

// 激活的标签页（follow 或 fans）
const activeTab = ref('follow');
// 搜索关键词
const searchKeyword = ref('');

// 从路由参数中获取默认标签
watch(() => route.query.tab, (newTab) => {
  if (newTab === 'fans') {
    activeTab.value = 'fans';
  } else {
    activeTab.value = 'follow';
  }
}, { immediate: true });

onMounted(() => {
  FollowerInit();
  FansInit();
});
// 模拟关注列表数据

const followList = ref([]);

// 模拟粉丝列表数据
const fansList = ref([]);

// 筛选后的关注列表（根据搜索关键词）
const filteredFollowList = computed(() => {
  if (!searchKeyword.value) return followList.value;
  return followList.value.filter(user =>
      user.nickname.includes(searchKeyword.value)
  );
});

// 筛选后的粉丝列表（根据搜索关键词）
const filteredFansList = computed(() => {
  if (!searchKeyword.value) return fansList.value;
  return fansList.value.filter(user =>
      user.nickname.includes(searchKeyword.value)
  );
});
//初始化关注列表
const FollowerInit = () =>{
  const follower_url='/api/follows/getFollowsById?id=' + route.params.id;
  axios.get(follower_url).then(res=>{
    followList.value=res.data;
    // console.log(res.data)
  })
}
//初始化粉丝列表
const FansInit = () =>{
  const fans_url='/api/follows/getFansById?id=' + route.params.id;
  axios.get(fans_url).then(res=>{
    fansList.value=res.data;
    // console.log(res.data)
  })
}
// 切换关注状态方法
const toggleFollow = (user, index) => {
  user.isFollowed = !user.isFollowed;
  // isCancel = 0表示关注，1表示取消关注
  let isCancel = user.isFollowed ? 0 : 1;
  // let follower = parseInt(user.followerId);
  // let fans = parseInt(user.followeeId);
  changeFollow(user.followerId, user.followeeId, isCancel);
  // console.log(`${user.name} 的关注状态变为：${user.isFollowed ? '已关注' : '未关注'}`);
};
//关注方法 fans = 根据followeeId查询得到followerId关注者的id
const changeFollow = (follower, fans, isCancel) =>{
  const changeFollowUrl = '/api/follows/updateFollowById?followerId=' + follower + '&followeeId=' + fans + '&isCancel=' + isCancel;
  axios.post(changeFollowUrl).then(res=>{
    console.log(res)
  })
}
// 返回上一页方法
const goBack = () => {
  // 这里可以根据实际路由情况进行返回操作，比如使用 router.back()
  router.push({
    path: '/profile'
  });
};
</script>

<style scoped>
.follow-page {
  width: 100%;
  height: 100vh;
  background: linear-gradient(135deg, #f5f7fa 0%, #e4edf9 100%);
  display: flex;
  flex-direction: column;
}

.header {
  display: flex;
  align-items: center;
  padding: 16px;
  background: linear-gradient(120deg, #ffffff, #f8f9ff);
  border-bottom: 1px solid #e5e5e5;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
}

.back-btn {
  background: none;
  border: none;
  cursor: pointer;
  padding: 8px;
  border-radius: 50%;
  transition: background-color 0.3s;
}

.back-btn:hover {
  background-color: #f1f5f9;
}

.tab-buttons {
  flex: 1;
  display: flex;
  justify-content: center;
}

.tab-btn {
  padding: 8px 16px;
  background: none;
  border: none;
  cursor: pointer;
  font-size: 16px;
  color: #64748b;
  font-weight: 500;
  position: relative;
  transition: color 0.3s;
}

.tab-btn.active {
  color: #667eea;
  font-weight: 600;
}

.tab-btn.active::after {
  content: '';
  position: absolute;
  bottom: -16px;
  left: 50%;
  transform: translateX(-50%);
  width: 40%;
  height: 3px;
  background: linear-gradient(90deg, #667eea, #764ba2);
  border-radius: 3px;
}

.search-bar {
  display: flex;
  align-items: center;
  padding: 12px 16px;
  background: linear-gradient(120deg, #ffffff, #f8f9ff);
  border-bottom: 1px solid #e5e5e5;
}

.search-input {
  flex: 1;
  padding: 12px 16px;
  border: 1px solid #e2e8f0;
  border-radius: 24px;
  outline: none;
  font-size: 14px;
  background-color: #f8fafc;
  transition: all 0.3s;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.05);
}

.search-input:focus {
  border-color: #667eea;
  background-color: #ffffff;
  box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
}

.search-clear-btn {
  background: none;
  border: none;
  cursor: pointer;
  padding: 4px;
  color: #94a3b8;
  border-radius: 50%;
  transition: background-color 0.3s;
  margin-left: 8px;
}

.search-clear-btn:hover {
  background-color: #f1f5f9;
  color: #64748b;
}

.list-content {
  flex: 1;
  padding: 16px;
  overflow-y: auto;
  background: linear-gradient(135deg, #f5f7fa 0%, #e4edf9 100%);
}

.user-list {
  list-style: none;
  padding: 0;
}

.user-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px;
  border-radius: 16px;
  margin-bottom: 16px;
  background: white;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
  transition: all 0.3s ease;
  border: 1px solid #f1f5f9;
}

.user-item:hover {
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.1);
  transform: translateY(-3px);
}

.user-avatar {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  object-fit: cover;
  border: 2px solid #f1f5f9;
}

.user-info {
  flex: 1;
  margin: 0 16px;
}

.user-name-level {
  display: flex;
  align-items: center;
  margin-bottom: 4px;
}

.name {
  font-weight: 600;
  margin-right: 8px;
  color: #1e293b;
  font-size: 15px;
}

.level-tag {
  padding: 2px 8px;
  border-radius: 12px;
  color: #fff;
  font-size: 12px;
  font-weight: 500;
}

.status {
  color: #64748b;
  font-size: 13px;
}

.follow-btn {
  padding: 6px 16px;
  border: 1px solid #667eea;
  border-radius: 20px;
  cursor: pointer;
  background: transparent;
  color: #667eea;
  font-weight: 500;
  transition: all 0.3s;
}

.follow-btn:hover {
  background-color: #667eea;
  color: white;
}

.follow-btn.followed {
  background-color: #667eea;
  color: white;
}

.follow-btn.followed:hover {
  background-color: #f8fafc;
  color: #667eea;
  border-color: #667eea;
}

</style>
